package top.yj95.xiets.basic;

import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.WindowConstants;

/**
 * 文章《<a href="https://xiets.blog.csdn.net/article/details/74363379" target="_balnk">JavaSwing_2.3: JRadioButton（单选按钮）</a>》中的示例代码
 * @author 杨军
 * @since 2023-12-22
 */
public class JRadioButtonDemo {
    
    public static void main(String[] args) {
        JFrame frame = new JFrame("测试窗口");
        frame.setSize(250, 200);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        
        JPanel panel = new JPanel();
        
        // 创建两个单选按钮
        JRadioButton radioBtn1 = new JRadioButton("男");
        JRadioButton radioBtn2 = new JRadioButton("女");
        
        // 创建按钮组，把两个单选按钮添加到该组
        ButtonGroup btnGroup = new ButtonGroup();
        btnGroup.add(radioBtn1);
        btnGroup.add(radioBtn2);
        
        // 设置第一个单选按钮选中
        radioBtn1.setSelected(true);
        
        panel.add(radioBtn1);
        panel.add(radioBtn2);
        
        frame.setContentPane(panel);
        frame.setVisible(true);
    }
    
}
